﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;

public partial class Shippers : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlParameter param;
    SqlDataAdapter da;
    DataSet ds;
    SqlCommandBuilder cb;
    DataView dv;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        initialconect();
        if (!IsPostBack)
        {
            gvShipper.AutoGenerateSelectButton = true;
            gvShipper.AllowPaging = true;
            gvShipper.PageSize = 7;
            LoadShipperDisconnect();
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnReload.Enabled = false;
            btnNew.Enabled = true;
            btnAdd.Enabled = true;
            txtShipperID.Enabled = false;
            Session["isselect"] = false;
        }
        bool tmp = (bool)Session["isselect"];
        if (tmp)
        {
            btnDelete.Enabled = true;
            btnUpdate.Enabled = true;
            btnAdd.Enabled = false;
        }
    }
    #region Initial
    void initialconect()
    {
        try
        {
            con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        }
        catch (Exception)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Connect To Server Fail');", true);
        }
    }
    void initialform()
    {
        if (IsPostBack)
        {
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnAdd.Enabled = true;
        }
        txtSearchName.Text = "";
        txtShipperID.Text = "";
        txtCompanyName.Text = "";
        txtPhone.Text = "";
    }
    void LoadShipperDisconnect()
    {
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Sales.Shippers";
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);
            gvShipper.DataSource = dt;
            gvShipper.DataBind();
        }
        catch (Exception)
        {
            Response.Write("<script>alert('" + "Load Data Fail !" + "')</script>");
        }
    }
    #endregion
    #region Function
    void AddShipper()
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_Shippers_Insert";
            param = new SqlParameter("@companyname", SqlDbType.NVarChar);
            param.Size = 40;
            param.Value = txtCompanyName.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@phone", SqlDbType.NVarChar);
            param.Size = 24;
            param.Value = txtPhone.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
    void DeleteShipper()
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_Shippers_Delete";
            param = new SqlParameter("@shipperid", SqlDbType.Int);
            param.Value = txtShipperID.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
    void UpdateShipper()
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_Shippers_Update";
            param = new SqlParameter("@shipperid", SqlDbType.Int);
            param.Value = txtShipperID.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@companyname", SqlDbType.NVarChar);
            param.Size = 40;
            param.Value = txtCompanyName.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@phone", SqlDbType.NVarChar);
            param.Size = 24;
            param.Value = txtPhone.Text;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        {
            con.Close();
        }
    } 
    #endregion

    #region Search
    void SearchShipper()
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format("SELECT * FROM Sales.Shippers WHERE companyname like '%{0}%'", txtSearchName.Text);
            da = new SqlDataAdapter(cmd);
            dt = new DataTable("Sales.Shippers");
            da.Fill(dt);
            gvShipper.DataSource = dt;
            gvShipper.DataBind();
            gvShipper.AutoGenerateSelectButton = true;
            gvShipper.AllowPaging = true;
            gvShipper.PageSize = 7;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    void Searchphone()
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format("SELECT * FROM Sales.Shippers WHERE phone like '%{0}%'", txtSearchName.Text);
            da = new SqlDataAdapter(cmd);
            dt = new DataTable("Sales.Shippers");
            da.Fill(dt);
            gvShipper.DataSource = dt;
            gvShipper.DataBind();
            gvShipper.AutoGenerateSelectButton = true;
            gvShipper.AllowPaging = true;
            gvShipper.PageSize = 7;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    } 
    #endregion

    #region Event
    protected void btnNew_Click(object sender, EventArgs e)
    {
        initialform();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bool check = true;
        try
        {
            AddShipper();
        }
        catch
        {
            check = false;
            Response.Write("<script>alert('" + "Add Fail !" + "')</script>");
        }
        finally
        {
            initialform();
        }
        if (check)
        {
            Response.Write("<script>alert('" + "Add Success !" + "')</script>");
            LoadShipperDisconnect();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool check = true;
        if (gvShipper.Rows.Count > 0)
        {
            try
            {
                UpdateShipper();
                initialform();
            }
            catch (Exception)
            {
                check = false;
                Response.Write("<script>alert('" + "Update Fail !" + "')</script>");
            }
            finally
            {
                initialform();
            }
            if (check)
            {
                Response.Write("<script>alert('" + "Update Success !" + "')</script>");
                LoadShipperDisconnect();
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        bool check = true;
        if (gvShipper.Rows.Count > 0)
        {
            try
            {
                DeleteShipper();
                initialform();
            }
            catch (Exception)
            {
                check = false;
                Response.Write("<script>alert('" + "Delete Fail !" + "')</script>");
            }
            finally
            {
                initialform();
            }
            if (check)
            {
                Response.Write("<script>alert('" + "Delete Success !" + "')</script>");
                LoadShipperDisconnect();
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool check = true;
        try
        {
            int tmp = cbSearch.SelectedIndex;
            if (tmp == 0)
                SearchShipper();
            if (tmp == 1)
                Searchphone();
            Session["issearch"] = true;
        }
        catch (Exception)
        {
            check = false;
            Response.Write("<script>alert('" + "Search Fail" + "')</script>");
        }
        if (!check || txtSearchName.Text.Trim() == "")
        {
            LoadShipperDisconnect();
        }
        else
        {
            btnReload.Enabled = true;
        }
    }
    protected void btnReload_Click(object sender, EventArgs e)
    {
        btnReload.Enabled = false;
        LoadShipperDisconnect();
        Session["issearch"] = false;
    }
    protected void gvShipper_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gvShipper.Rows.Count > 0)
        {
            try
            {
                if (IsPostBack)
                {
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    btnAdd.Enabled = false;
                }
                GridViewRow r = gvShipper.SelectedRow;
                txtShipperID.Text = Server.HtmlDecode(r.Cells[1].Text.ToString());
                txtCompanyName.Text = Server.HtmlDecode(r.Cells[2].Text.ToString());
                txtPhone.Text = Server.HtmlDecode(r.Cells[3].Text.ToString());
                Session["isSelected"] = true;
            }
            catch (Exception)
            {
                Response.Write("<script>alert('" + "Fails Page! F5 to Reload" + "')</script>");
            }
        }
    }
    protected void gvShipper_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (!(bool)Session["issearch"])
            {
                gvShipper.PageIndex = e.NewPageIndex;
                LoadShipperDisconnect();
                
            }
            else
            {
                gvShipper.PageIndex = e.NewPageIndex;
                btnSearch_Click(sender, e);
            }
        }
        catch (Exception)
        {
            Response.Write("<script>alert('" + "Fails Page! F5 to Reload" + "')</script>");
        }
    } 
    #endregion
}
